package com.alibaba.csp.sentinel.dashboard.repository.metric.clickhouse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.clickhouse.client.internal.google.gson.JsonElement;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 埋点基础事件(TBaseEvent)表数据库访问层
 *
 * @author EasyCode
 * @since 2023-09-18 10:02:18
 */
@DS("clickhouse_1")
public interface TSecondsMetricDao {
    @Select("<script> " +
            "select * from sentinel.t_seconds_metric final where app=#{app}" +
            "<if test='resource!=null'>" +
            " and resource=#{resource} " +
            "</if>" +
            "and `timestamp` between #{startTime} and #{endTime} order by `timestamp`" +
            "</script>  ")
    List<TSecondsMetric> selectByAppAndResourceBetween(@Param("app") String app,@Param("resource") String resource,@Param("startTime") Date startTime, @Param("endTime") Date endTime);

//    @Insert("<script> " +
//            "insert into sentinel.t_seconds_metric(app, resource, timestamp, passQps, blockQps, successQps, exceptionQps, rt, count) values " +
//            "<foreach collection='secondsMetrics' separator=',' item='metric'> " +
//            " ('${metric.app}','${metric.resource}',${metric.timestamp.getTime()},${metric.passQps},${metric.blockQps},${metric.successQps},${metric.exceptionQps},${metric.rt},${metric.count}) " +
//            "</foreach> " +
//            "</script> "
//    )
//    Integer insertBatch(@Param("secondsMetrics") List<TSecondsMetric> secondsMetrics);

    @Insert("INSERT INTO sentinel.t_seconds_metric  format JSONEachRow #{jsonEachRow}")
    Integer insertBatchJson(@Param("jsonEachRow") JsonElement jsonEachRow);

}

